package algorithm;
import java.util.ArrayList;
import java.util.List;
import bean.Dynamic_information;
import bean.Credit_parameter;
import java.util.Calendar;;
public class Dynamic_information_select {
	//24小时
	public static final int Twenty_four_hours = 0;
	//一周
	public static final int A_Week = 1;
	public List<Dynamic_information> Get_present_information(List<Dynamic_information> dy,int flag){
		
		List<Dynamic_information> dynamic_array = new ArrayList<Dynamic_information>();
		List<Dynamic_information> result_array = new ArrayList<Dynamic_information>();
		//将list里的静态dynamic_information复制到dynamic_array
		for(int i = 0;i < dy.size();i++){
			dynamic_array.add(dy.get(i));
		}
		/*
		for(Dynamic_information dynamic_information : dynamic_array){
			System.out.print(dynamic_information.getContent() + " ");
		}
		System.out.println(" ");
		*/
		int length1 = dynamic_array.size();
		if(flag == Twenty_four_hours){      //标志符flag为1天时
			for(int i = 0;i < length1;i++){   
				Calendar now_ca = Calendar.getInstance();
				Calendar past_ca = Calendar.getInstance();
				past_ca.setTime(dynamic_array.get(i).getDate());
				int nowday = now_ca.get(Calendar.DAY_OF_YEAR);
				int nowyear = now_ca.get(Calendar.YEAR);
				int pastday = past_ca.get(Calendar.DAY_OF_YEAR);
				int pastyear = past_ca.get(Calendar.YEAR);
				if((nowday - pastday > 1 && nowyear == pastyear) ||
				   ( (nowday > 1 || pastday < 365) && nowyear - pastyear == 1) ||
				   (nowyear - pastyear > 1)){
					dynamic_array.remove(i);
					i--;
					length1--;
				}
			}
		}
		else if(flag == A_Week){             //标志符flag为1周时
			for(int i = 0;i < length1;i++){ 
				Calendar now_ca = Calendar.getInstance();
				Calendar past_ca = Calendar.getInstance();
				past_ca.setTime(dynamic_array.get(i).getDate());
				int nowweek = now_ca.get(Calendar.WEEK_OF_YEAR);
				int nowyear = now_ca.get(Calendar.YEAR);
				int pastweek = past_ca.get(Calendar.WEEK_OF_YEAR);
				int pastyear = past_ca.get(Calendar.YEAR);
				if((nowweek - pastweek  > 1 && nowyear == pastyear) || 
				   (nowweek > 1 && pastweek < 52 && nowyear - pastyear == 1) ||
				   (nowweek == 52 && pastweek == 1 && nowyear - pastyear == 1) ||
				   (nowyear - pastyear > 1) ){
					dynamic_array.remove(i);
					i--;
					length1--;
				}
			}
		}
		else{
			;
		}
		/*
		for(Dynamic_information dynamic_information : dynamic_array){
			System.out.print(dynamic_information.getContent() + " ");
		}
		*/
		int length2 = dynamic_array.size();
		double score[] = new double[length2];           //attention，forward，review，praise积分统计
		for(int i = 0;i < length2;i++) { 
			int attention = dynamic_array.get(i).getAttention_number();
			int forward = dynamic_array.get(i).getForword_number();
			int review = dynamic_array.get(i).getReview_number();
			int praise = dynamic_array.get(i).getPraise_number();
			if(attention < Credit_parameter.getAttention_log()){
				attention = 1;
			}
			if(forward < Credit_parameter.getForward_log()){
				forward = 1;
			}
			if(review < Credit_parameter.getReview_log()){
				review = 1;
			}
			if(praise < Credit_parameter.getPraise_log()){
				praise = 1;
			}
			score[i] = Math.log(attention) / Math.log(Credit_parameter.getAttention_log()) +
					   Math.log(forward) / Math.log(Credit_parameter.getForward_log()) +
					   Math.log(review) / Math.log(Credit_parameter.getReview_log()) + 
					   Math.log(praise) / Math.log(Credit_parameter.getPraise_log());
		}
		
		
		//排序至多TOP20，使用冒泡排序
		if(length2 > 20)
			length2 = 20;
		
		int id_array[] = new int[length2];
		for(int i = 0;i < length2;i++){
			id_array[i] = 0;
		}
		
		for(int i = 0;i < length2;i++){
			for(int j = 0;j < dynamic_array.size();j++){
				if(score[j] > score[ id_array[i] ]){
					id_array[i] = j;
				}
			}
			score[ id_array[i] ] = -1;
		}
		
		for(int i = 0;i < length2;i++){                      //输出dynamic_array
			result_array.add(dynamic_array.get(id_array[i]));
		}
		/*
		System.out.println(" ");
		for(Dynamic_information dynamic_information : result_array){
			System.out.print(dynamic_information.getContent() + " ");
			System.out.println(" ");
		}*/
		return result_array;
	}

}
